<?php
/**
 * Created by PhpStorm.
 * User: zhaozhichao
 * Date: 2018/6/3
 * Time: 下午6:00
 */


if (!defined('IN_ANWSION')) {
    die();
}

class goods extends AWS_ADMIN_CONTROLLER
{

//    public function setup()
//    {
//        //设置左侧导航选中
//
//
//        TPL::assign('system_group', $this->model('account')->get_user_group_list(0));
//
//    }

    /**
     *
     * 商品分类管理
     */
    public  function category_list_action(){

        $this->crumb(AWS_APP::lang()->_t('商品分类'), 'admin/goods/category_list/');

        TPL::assign('list', json_decode($this->model('system')->build_category_json('goods'), true));

        TPL::assign('category_option', $this->model('system')->build_category_html('goods', 0, 0, null, false));

        TPL::assign('target_category', $this->model('system')->build_category_html('goods', 0, null));

        TPL::assign('menu_list', $this->model('admin')->fetch_menu_list(1011));

        TPL::output('admin/goods/category_list');
    }
    /**
     *
     * 专题管理
     */
    public  function special_action(){

        $this->crumb(AWS_APP::lang()->_t('商品专题'), 'admin/goods_special/list/');
        TPL::assign('menu_list', $this->model('admin')->fetch_menu_list(1012));

        if ($_POST['action'] == 'search')
        {
            foreach ($_POST as $key => $val)
            {
                if (in_array($key, array('title')))
                {
                    $val = rawurlencode($val);
                }

                $param[] = $key . '-' . $val;
            }

            H::ajax_json_output(AWS_APP::RSM(array(
                'url' => get_js_url('/admin/goods/special/' . implode('__', $param))
            ), 1, null));
        }

        $where = array();
        if ($_GET['type'] == 'forbidden') {
            $where[] = 'state = 0';
        }
        if ($_GET['title']) {
            $where[] = "title LIKE '%" . $this->model('goodsSpecial')->quote($_GET['title']) . "%'";
        }
        $list = $this->model('goodsSpecial')->fetch_page('goods_special', implode(' AND ', $where), 'id DESC', $_GET['page'], $this->per_page);

        $total_rows = $this->model('goodsSpecial')->found_rows();

        $url_param = array();

        foreach ($_GET as $key => $val) {
            if (!in_array($key, array('app', 'c', 'act', 'page'))) {
                $url_param[] = $key . '-' . $val;
            }
        }
        TPL::assign('pagination', AWS_APP::pagination()->initialize(array(
            'base_url' => get_js_url('/admin/goods_special/list') . implode('__', $url_param),
            'total_rows' => $total_rows,
            'per_page' => $this->per_page
        ))->create_links());

        TPL::assign('list', $list);
        TPL::assign('total_rows', $total_rows);

        TPL::output('admin/goods_special/list');
    }

    /**
     * add专题
     */
    public function special_add_action(){

        $this->crumb(AWS_APP::lang()->_t('添加专题'), 'admin/goods_special/add');
        TPL::assign('menu_list', $this->model('admin')->fetch_menu_list(1012));

        TPL::output('admin/goods_special/edit');

    }
    /**
     * 专题编辑
     */
    public function special_edit_action(){


        $this->crumb(AWS_APP::lang()->_t('编辑专题'), 'admin/goods/special_edit');

        TPL::assign('menu_list', $this->model('admin')->fetch_menu_list(1012));

        if (!$special = $this->model('goodsSpecial')->get_special_info_by_id($_GET['id'], TRUE))
        {
            H::redirect_msg(AWS_APP::lang()->_t('商品不存在'), '/admin/goods/special/');
        }

        TPL::assign('special', $special);


        TPL::output('admin/goods_special/edit');

    }
    /**
     * 商品列表
     */
    public function goods_list_action()
    {
        TPL::assign('menu_list', $this->model('admin')->fetch_menu_list(1010));


        if ($_POST['action'] == 'search')
        {
            foreach ($_POST as $key => $val)
            {
                if (in_array($key, array('goods_name')))
                {
                    $val = rawurlencode($val);
                }

                $param[] = $key . '-' . $val;
            }

            H::ajax_json_output(AWS_APP::RSM(array(
                'url' => get_js_url('/admin/goods/goods_list/' . implode('__', $param))
            ), 1, null));
        }

        $this->crumb(AWS_APP::lang()->_t('商品管理'), 'admin/goods/goods_list/');

        $where = array();
        if ($_GET['type'] == 'forbidden') {
            $where[] = 'is_on_sale = 0';
        }
        if ($_GET['goods_name']) {
            $where[] = "goods_name LIKE '%" . $this->model('goods')->quote($_GET['goods_name']) . "%'";
        }
        if(intval($_GET['cat_id'])){
            $where[] = 'cat_id = ' . intval($_GET['cat_id']);
        }

        $goods_list = $this->model('goods')->fetch_page('goods', implode(' AND ', $where), 'goods_id DESC', $_GET['page'], $this->per_page);

        $total_rows = $this->model('goods')->found_rows();

        $url_param = array();

        foreach ($_GET as $key => $val) {
            if (!in_array($key, array('app', 'c', 'act', 'page'))) {
                $url_param[] = $key . '-' . $val;
            }
        }
        TPL::assign('pagination', AWS_APP::pagination()->initialize(array(
            'base_url' => get_js_url('/admin/goods/goods_list/') . implode('__', $url_param),
            'total_rows' => $total_rows,
            'per_page' => $this->per_page
        ))->create_links());

        TPL::assign('category_list', $this->model('system')->build_category_html('goods', 0,'', null, true));

        TPL::assign('list', $goods_list);
        TPL::assign('total_rows', $total_rows);


        TPL::output('admin/goods/goods_list');

    }

    /**
     * 编辑商品
     */
    public function edit_action()
    {
        $this->crumb(AWS_APP::lang()->_t('编辑商品资料'), 'admin/goods/edit/');

        if (!$goods = $this->model('goods')->get_goods_info_by_gid($_GET['goods_id'], TRUE))
        {
            H::redirect_msg(AWS_APP::lang()->_t('商品不存在'), '/admin/goods/goods_list/');
        }

//        if ($user['group_id'] == 1 AND !$this->user_info['permission']['is_administortar'])
//        {
//            H::redirect_msg(AWS_APP::lang()->_t('你没有权限编辑管理员账号'), '/admin/user/list/');
//        }

//        TPL::assign('job_list', $this->model('work')->get_jobs_list());
//        TPL::assign('mem_group', $this->model('account')->get_user_group_by_id($user['reputation_group']));
//        TPL::assign('system_group', $this->model('account')->get_user_group_list(0));



        $goods_color=array(
            ''=>'',
            '#CFB8B0'=>'琥珀棕',
            '#DBC7AF'=>'卡其色',
            '#B4A698'=>'香槟色',
            '#63504C'=>'琥珀棕',
            '#D95965'=>'黑胡桃',
            '#B6413D'=>'玫瑰红',
            '#272727'=>'夜鹰黑',
            '#CED1D0'=>'银色',
            '#CD9E76'=>'自然榉木',
            '#917467'=>'棕色',
            '#616F7C'=>'银灰色',
            '#8E3434'=>'红色',
            '#F6F4F5'=>'珍珠白',
            '#6E262F'=>'酒红A',
            '#C3575A'=>'酒红B',
            '#D95965'=>'酒红C',
            '#FF937C'=>'酒红D',
        );

        TPL::assign('goods_color',$goods_color);
//        商品规格
        TPL::assign('goods_style_list',$this->model('goods')->get_goods_style_list($goods['goods_id']));

        TPL::assign('goods', $goods);

        TPL::assign('menu_list', $this->model('admin')->fetch_menu_list(1010));

        TPL::assign('category_list', $this->model('system')->build_category_html('goods', 0, $goods['cat_id'], null, true));

        TPL::assign('attach_access_key', md5($this->user_id . time()));

        TPL::output('admin/goods/edit');
    }

    /**
     *
     * 添加商品控制器
     */
    public function goods_add_action()
    {
        TPL::assign('menu_list', $this->model('admin')->fetch_menu_list(1010));

        $this->crumb(AWS_APP::lang()->_t('添加商品'), "admin/goods/goods/goods_add/");

        TPL::assign('category_list', $this->model('system')->build_category_html('goods', 0, 0, null, true));

        TPL::output('admin/goods/add');
    }
}

